//
//  DiscoverViewController.m
//  Mtime2.0
//
//  Created by 凉气的Mac on 16/9/20.
//  Copyright © 2016年 凉气的Mac. All rights reserved.
//

#import "DiscoverViewController.h"

#import "NewsTableView.h"

#import "TrailerTableView.h"

#import "ChartsTableView.h"

#import "ReviewTableView.h"

@interface DiscoverViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)  NSMutableArray *btnArray;

@property(nonatomic,weak) UIButton *selectBtn;

@property(nonatomic,weak) UIView *lineView;

@property(nonatomic,weak) UIScrollView *scrollView;

@end

@implementation DiscoverViewController

-(NSMutableArray *)btnArray{

    if (_btnArray == nil) {
        
        _btnArray = [NSMutableArray array];
        
    }

    return _btnArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavigationBar];
    
    [self setupUI];
    
}

#pragma mark --
#pragma mark --设置导航栏
-(void)setupNavigationBar{

    self.navigationController.navigationBar.hidden = YES;
    
    UIView *navView = [[UIView alloc]init];
    
    navView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"menu_top_bg7"]];
    
    [self.view addSubview:navView];
    
    [navView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        
        make.height.equalTo(@49);
        
    }];
    
    NSArray *titleArray = @[@"新闻",@"预告片",@"排行榜",@"影评"];
    
    UIButton *tempBtn;
    
    for (int i = 0; i < titleArray.count ; i++) {
        
        UIButton *button  = [[UIButton alloc]init];
        
        [button addTarget:self action:@selector(clickNavBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = i;
        
        [self.btnArray addObject:button];
        
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        [navView addSubview:button];
        
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.bottom.equalTo(navView);
            
            make.width.equalTo(navView).multipliedBy(0.25);
            
            make.left.equalTo(tempBtn?tempBtn.mas_right:navView);
            
        }];
        
        tempBtn = button;
        
    }
    
    UIView *lineView = [[UIView alloc]init];
    
    self.lineView = lineView;
    
    lineView.backgroundColor = [UIColor whiteColor];
    
    [navView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(tempBtn);
        
        make.left.bottom.equalTo(navView);
        
        make.height.equalTo(@2);
        
    }];
    
}

-(void)setupUI{

    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    scrollView.delegate = self;
    
    scrollView.pagingEnabled = YES;
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.bounces = NO;
    
    scrollView.contentSize = CGSizeMake(kScreen.size.width * 4, 0);
    
    self.scrollView = scrollView;
    
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.left.right.equalTo(self.view);
        
        make.top.equalTo(self.view).offset(49);
        
    }];
    
    NewsTableView *news = [[NewsTableView alloc]init];
    
    TrailerTableView *trailer = [[TrailerTableView alloc]init];
    
    ChartsTableView *charts = [[ChartsTableView alloc]init];
    
    ReviewTableView *review = [[ReviewTableView alloc]init];
    
    NSArray *tableViewArray = @[news,trailer,charts,review];
    
    UITableView *tempTable;
    
    for (int i = 0; i<tableViewArray.count; i++) {
        
        UITableView *tableView = tableViewArray[i];
        
        [scrollView addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.width.equalTo(self.view);
            
            make.top.equalTo(self.view).offset(49);
            
            make.left.equalTo(tempTable?tempTable.mas_right:scrollView);
            
        }];
        
        tempTable = tableView;
        
    }
    
}


#pragma mark --
#pragma mark --导航栏按钮点击事件
-(void)clickNavBtn:(UIButton *)button{

    if (button.tag == self.selectBtn.tag) {
        
        return;
    }
    
    
    
    [self.scrollView setContentOffset:CGPointMake(button.tag * self.scrollView.width, 0) animated:YES];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.lineView.transform = CGAffineTransformTranslate(self.selectBtn.transform, button.x, 0);
    }];
    
    [self.selectBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.selectBtn = button;
    
}

#pragma mark --
#pragma mark --scrollView代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.lineView.transform = CGAffineTransformTranslate(self.selectBtn.transform,scrollView.contentOffset.x / 4, 0);

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSInteger index = scrollView.contentOffset.x / kScreen.size.width;
    
    UIButton *button = self.btnArray[index];
    
    [self clickNavBtn:button];

}


@end




























